package com.quanyan.ic.protocol.codec.protobuf;

import com.quanyan.ic.common.model.OrderMessage;
import com.quanyan.ic.protocol.Code;
import com.quanyan.ic.protocol.LightCmd;
import com.quanyan.ic.protocol.MessageType;

/**
 * Created by zhuo.shi on 2017/6/2.
 */
public class ICMessageBuilder {

    /**
     * 构造握手消息
     */
    public static ICMessageProto.ICMessage buildHandShakeMsg(int pid, Code respCode) {
        ICMessageProto.ICMessage.Builder builder = ICMessageProto.ICMessage.newBuilder();
        ICMessageProto.ICMessage.Header.Builder header = ICMessageProto.ICMessage.Header.newBuilder();
        header.setProtocolVersion(0xAF1);
        header.setType(MessageType.HANDSHAKE_RESP.value());
        header.setPid(pid);
        header.setRespCode(respCode.value());
        builder.setHeader(header.build());
        return builder.build();
    }

    /**
     * 构造心跳消息
     */
    public static ICMessageProto.ICMessage buildHeatBeatMsg(int placeId, MessageType messageType) {
        ICMessageProto.ICMessage.Builder builder = ICMessageProto.ICMessage.newBuilder();
        ICMessageProto.ICMessage.Header.Builder hbuilder = ICMessageProto.ICMessage.Header.newBuilder();
        hbuilder.setProtocolVersion(0xAF1);
        hbuilder.setType(messageType.value());
        hbuilder.setRespCode(Code.RC_200.value());
        hbuilder.setPid(placeId);
        builder.setHeader(hbuilder.build());
        return builder.build();
    }

    /**
     * 构造开关灯消息
     */
    public static ICMessageProto.ICMessage buildBizICMessage(OrderMessage orderMessage, LightCmd lightCmd) {
        ICMessageProto.ICMessage.Builder builder = ICMessageProto.ICMessage.newBuilder();
        ICMessageProto.ICMessage.Header.Builder hbuilder = ICMessageProto.ICMessage.Header.newBuilder();
        hbuilder.setProtocolVersion(0xAF1);
        hbuilder.setPid(orderMessage.getPlaceId());
        hbuilder.setType(MessageType.SERVICE_REQ.value());

        ICMessageProto.ICMessage.Body.Builder bbuilder = ICMessageProto.ICMessage.Body.newBuilder();
        bbuilder.setOrderNo(orderMessage.getOrderNo());
        bbuilder.setStime(orderMessage.getStartTime());
        bbuilder.setEtime(orderMessage.getEndTime());
        bbuilder.setOrderStatus(orderMessage.getOrderStatus());
        bbuilder.setSiteType(orderMessage.getSiteType());
        bbuilder.setSid(orderMessage.getSiteId());
        bbuilder.setTf(lightCmd.value());
        bbuilder.setSc(Code.DEFAULT.value());
        builder.setHeader(hbuilder.build());
        builder.setBody(bbuilder.build());

        return builder.build();
    }

//    /**
//     *
//     * @return
//     */
//    public static ICMessageProto.ICMessage buildLightStatusMessage(int placeId, int siteId, byte lightCmd, Code resCode){
//        ICMessageProto.ICMessage.Builder builder = ICMessageProto.ICMessage.newBuilder();
//        ICMessageProto.ICMessage.Header.Builder hbuilder = ICMessageProto.ICMessage.Header.newBuilder();
//        hbuilder.setProtocolVersion(0xAF1);
//        hbuilder.setPid(placeId);
//        hbuilder.setType(MessageType.LIGHTSTATUS_RESP.value());
//
//        ICMessageProto.ICMessage.Body.Builder bbuilder = ICMessageProto.ICMessage.Body.newBuilder();
//        bbuilder.setSid(siteId);
//        bbuilder.setTf(lightCmd);
//        bbuilder.setOrderNo("");
//        bbuilder.setSc(resCode.value());
//        builder.setHeader(hbuilder.build());
//        builder.setBody(bbuilder.build());
//
//        return builder.build();
//    }
}
